unit ServiceU;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, SvcMgr,
  Vcl.ExtCtrls,FMX.Forms;

type
  TWinService = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceAfterInstall(Sender: TService);
    procedure ServiceCreate(Sender: TObject);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

{$R *.dfm}


var
  WinService: TWinService;

implementation

uses
  System.Win.Registry, ConfingU, MainModuU,MVCFramework.Logger;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  WinService.Controller(CtrlCode);
end;

function TWinService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TWinService.ServiceAfterInstall(Sender: TService);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\SYSTEM\CurrentControlSet\Services\' + CfgServiceId, false) then
    begin
      Reg.WriteString('Description', CfgServiceDescription);
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TWinService.ServiceCreate(Sender: TObject);
begin
  AllowPause := False;

  Name := CfgServiceId;
  DisplayName := CfgServiceName;
  LogI('ServiceCreate');
end;

procedure TWinService.ServiceStart(Sender: TService; var Started: Boolean);
begin
  LogI('MainModule.DoStart');
  Started := MainModule.DoStart(True);
end;

procedure TWinService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
  Stopped := MainModule.DoStop(True);
end;

end.
